package com.qumitech.visualnovel.service.activity.impl;

import com.alibaba.fastjson.JSON;
import com.qumitech.visualnovel.component.util.HttpInvokeUtil;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.UrlConstant;
import com.qumitech.visualnovel.pubdef.dto.activity.ActivityUserSkillPriceDTO;
import com.qumitech.visualnovel.service.activity.ActivitySkillService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class ActivitySkillServiceImpl implements ActivitySkillService {
    private static final String ACTIVITY_SKILL_URL_PREFIX = "/admin/activity/skill/price/cfg";

    @Override
    public BaseRsp saveUserSkillPrice(ActivityUserSkillPriceDTO priceDTO) {
        String url = UrlConstant.SERVER_URL + ACTIVITY_SKILL_URL_PREFIX;
        String body = HttpInvokeUtil.httpPostJson(url, JSON.toJSONString(priceDTO),UrlConstant.DefaultEncode,null);
        return  JSON.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseRsp updateUserSkillPrice(ActivityUserSkillPriceDTO priceDTO) {
        String url = UrlConstant.SERVER_URL + ACTIVITY_SKILL_URL_PREFIX;
        String body = HttpInvokeUtil.httpPutJson(url, JSON.toJSONString(priceDTO),UrlConstant.DefaultEncode,null);
        return  JSON.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseRsp listUserSkillPrice(Long uid, Integer skillId, int offset, int limit) {
        String url = UrlConstant.SERVER_URL + ACTIVITY_SKILL_URL_PREFIX;
        Map<String, Object> req = new HashMap<>();
        if (uid != null){
            req.put("uid", uid);
        }
        if (skillId != null){
            req.put("skill_id", skillId);
        }
        req.put("offset", offset);
        req.put("limit", limit);
        String body = HttpInvokeUtil.httpGet(url, req);
        return  JSON.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseRsp rmUserSkillPrice(long id) {
        String url = UrlConstant.SERVER_URL + ACTIVITY_SKILL_URL_PREFIX + "/id/" + id;
        String body = HttpInvokeUtil.httpDelete(url, new HashMap<>());
        return JSON.parseObject(body, BaseRsp.class);
    }
}
